package fr.norsys.mapper.console.web;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import uk.ltd.getahead.dwr.ExecutionContext;
import fr.norsys.mapper.console.model.Application;
import fr.norsys.mapper.console.model.Attribute;
import fr.norsys.mapper.console.model.Connection;
import fr.norsys.mapper.console.model.ConsoleCst;
import fr.norsys.mapper.console.model.Resource;
import fr.norsys.mapper.console.service.ApplicationService;
import fr.norsys.mapper.console.service.LdapTreeBuilder;
import fr.norsys.mapper.console.service.ResourceService;

public class ResourceServiceDWR {
	private ResourceService resourceService;
	private ApplicationService applicationService;
	private LdapTreeBuilder ldapTreeBuilder;
	
	public LdapTreeBuilder getLdapTreeBuilder() {
		return ldapTreeBuilder;
	}
	public void setLdapTreeBuilder(LdapTreeBuilder ldapTreeBuilder) {
		this.ldapTreeBuilder = ldapTreeBuilder;
	}
	/**
	 * Action to save a resource in session
	 * @param resource
	 */
	public void save(Resource res, String objectClass)  throws Exception {
		Resource resource = res;
		Application currentAppli = (Application)ExecutionContext.get().getSession().getAttribute(ConsoleCst.CURRENT_APPLICATION);
		resourceService.save(resource, currentAppli);
		Resource currentResource = (Resource) ExecutionContext.get().getSession().getAttribute(ConsoleCst.CURRENT_RESOURCE);
		if(currentResource != null && ConsoleCst.ADD_MAPPER_TYPE.equals(currentResource.getType())) {
			addMandatoryAttributes(currentResource,currentAppli.getConnection(),objectClass);
		}
		ExecutionContext.get().getSession().setAttribute(ConsoleCst.IS_APPLICATION_MODIFIED,ConsoleCst.APPLICATION_MODIFIED);
	}
	private void addMandatoryAttributes(Resource r,Connection connection, String objectClass)  throws Exception {
		ldapTreeBuilder.setConnection(connection);
		if(r.getBaseDn() != null && !"".equals(r.getBaseDn().trim())) {
			Set s = ldapTreeBuilder.getMandatoryAttributes(r.getBaseDn());
			if(s!=null) {
				for(Iterator it=s.iterator();it.hasNext();) {
					String name = (String)it.next();
					Attribute attr = getAttribute(name,r.getAttributes());
					if(attr != null) {
						if("objectClass".equals(name))
							attr.setDefaultValue(objectClass);
					} else {
						Attribute a = new Attribute(name,name,ConsoleCst.INPUT_OUTPUT_ATTRIBUTE_TYPE);
						r.addAttribute(a);
						if("objectClass".equals(name))
							a.setDefaultValue(objectClass);
					}
				}
			}
		}
	}
	private Attribute getAttribute(String name,Collection attributes)  throws Exception {
		Attribute ret = null;
		for(Iterator it2=attributes.iterator();it2.hasNext();) {
			Attribute a = (Attribute) it2.next();
			if(name.equals(a.getAttributeLDAP())) {
				ret = a;
			}
		}
		return ret;
	}
	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}


	public ApplicationService getApplicationService() {
		return applicationService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

}
